﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GentlyWeep
{
     public class RecGuitarNote
     {
         private static RecGuitarNote instance;
         private RecGuitarNote() { }
         public static RecGuitarNote GetInstance()
         {
             lock (typeof(RecGuitarNote))
             {
                 if (instance == null)
                 {
                     instance = new RecGuitarNote();
                 }
                 return instance;
             }
         }
        List<string> notetemp = new List<string>();
        List<System.TimeSpan> timetemp = new List<System.TimeSpan>();
        List<System.TimeSpan> sub = new List<System.TimeSpan>();
        List<string> allnote = new List<string>();
        List<string> strum = new List<string>();
        List<string> noteName = new List<string>();
        List<double> amplitude = new List<double>();
        List<double> octavelevel = new List<double>();
        

        int[] key_counter={0,0,0,0,0,0,0,0,0,0,0,0};
        string noteStr = "";
        System.TimeSpan deadair ;
        System.TimeSpan start ;
        System.TimeSpan end;
        double threshold = 8.50;
        string status = "";

        public void setAmp(double amp)
        {
            amplitude.Add(amp);
            //Console.WriteLine("setCount = "+amplitude.Count);
            //Console.WriteLine("amp = " + amplitude[amplitude.Count - 1]);
        }



        public string getAmp()
        {
            //Console.WriteLine("getCount = " + amplitude[amplitude.Count - 1]);
            if (amplitude[amplitude.Count - 1] >= threshold)
            {
                status = "s";
            }
            else
            {
                status = "n";
            }
            return status;
        }

        public void setSensitive(double sense)
        {
            this.threshold = sense;
            //Console.WriteLine("setCount = "+amplitude.Count);
            //Console.WriteLine("amp = " + amplitude[amplitude.Count - 1]);
        }



        public double getSensitive()
        {
            //Console.WriteLine("getCount = " + amplitude[amplitude.Count - 1]);
            return threshold;
        }
      
        public void recNote(int note,string status,double octave,double closeFreq,string notename)
        {
            noteStr = note.ToString();
            key_counter[note % 12] += 1;
            notetemp.Add(noteStr);
            timetemp.Add(DateTime.Now.TimeOfDay);
            noteName.Add(notename);
            //Console.WriteLine("All note :"+notetemp.Count);
            strum.Add(status);
            octavelevel.Add(findOctave(closeFreq,octave));
            Console.WriteLine("Freq : "+closeFreq);
            if (timetemp.Count > 1)
            {
                start = timetemp[timetemp.Count - 2];
                end = timetemp[timetemp.Count-1];
                deadair = end.Subtract(start);
                sub.Add(deadair);
            }
        }

        public double findOctave(double closetFreq,double octave)
        {
            return Math.Round((Math.Log(closetFreq / octave))/Math.Log(2),1);
        }

        public List<string> getNote()
        {
            Console.WriteLine("______________________________");
            int currentPoint;

            if (allnote.Count == 0)
            {
                currentPoint = 0;
            }

            else
            {
                currentPoint = allnote.Count - 1;
            }

            if(timetemp.Count == notetemp.Count)
            {
                for (int i = currentPoint; i < timetemp.Count; i++)
                {
                    if (sub.Count != i)
                    {
                        allnote.Add(noteName[i]+"|" + notetemp[i] + "|" + octavelevel[i] + "|" + sub[i].Seconds + "|" + sub[i].Milliseconds + "|" + strum[i]);
                    }
                    else
                    {
                        allnote.Add(noteName[notetemp.Count - 1] + "|" + notetemp[notetemp.Count - 1] + "|" + octavelevel[notetemp.Count - 1] + "|" + 00 + "|" + 00 + "|" + strum[notetemp.Count - 1]);
                    }
                    //Console.WriteLine(allnote[i]);
                }
            }
            Console.WriteLine("allnote in getnote :"+allnote.Count);
            return allnote;
        }

        public int[] getKey()
        {
            int count = 0;
            int[] ans_key = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
            for (count = 0; count < 12; count++)
            {
                ans_key[count] = key_counter[(count + 1)%12] + 
                    key_counter[(count + 3)%12] + key_counter[(count + 6)%12] + 
                    key_counter[(count + 8)%12] + key_counter[(count + 10)%12];
            }
            Array.Sort(ans_key);
            return ans_key;
        }


    }
}
